const { app, BrowserWindow, webContents, ipcMain, Menu } = require("electron");
const path = require("node:path");
const store = require("./js/store");
const package = require("./package.json");

let win;
let tipWin;
let settingWin;
const createWindows = () => {
  win = new BrowserWindow({
    width: 1120,
    height: 610,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
      contextIsolation: false,
      preload: path.join(__dirname, "preload.js"),
    },
  });
  let menuTemp = [];
  let menu = Menu.buildFromTemplate(menuTemp);
  Menu.setApplicationMenu(menu);

  require("@electron/remote/main").initialize(); //为了使用remote的文件选择对话框功能
  require("@electron/remote/main").enable(win.webContents); //为了使用remote的文件选择对话框功能

  win.loadFile("index.html");
  if (!app.isPackaged) win.webContents.openDevTools(); //启动自动打开控制台

  //补丁--使用dialog，出现输入框无法正常输入问题
  const isWindows = process.platform === "win32";
  let needsFocusFix = false;
  let triggeringProgrammaticBlur = false;
  win.on("close", (_) => {
    tipWin && tipWin.close();
    settingWin && settingWin.close();
  });
  win.on("blur", (event) => {
    if (!triggeringProgrammaticBlur) {
      needsFocusFix = true;
    }
  });

  win.on("focus", (event) => {
    if (isWindows && needsFocusFix) {
      needsFocusFix = false;
      triggeringProgrammaticBlur = true;
      setTimeout(function () {
        setTimeout(function () {
          triggeringProgrammaticBlur = false;
        }, 100);
      }, 100);
    }
  });
};

app.whenReady().then(() => {
  createWindows();
  readStore();
  // 仅 macOS 支持
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindows();
    }
  });
});
app.on("window-all-closed", () => {
  // 对于 Mac 系统， 关闭窗口时并不会直接退出应用， 此时需要我们来手动处理
  if (process.platform !== "darwin") {
    app.quit();
  }
});

function showTipWindow() {
  tipWin = new BrowserWindow({
    width: 340,
    height: 400,
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true,
    },
  });
  tipWin.on("closed", () => {
    tipWin = null;
  });
  tipWin.loadFile("tip.html");
}

function readStore() {
  if (!store.get("first")) {
    showTipWindow();
    store.set("first", { version: package.version });
  } else if (store.get("first").version !== package.version) {
    showTipWindow();
    store.delete("first");
    store.set("first", { version: package.version });
  }
  if (!store.get("setting")) {
    store.set("setting", {
      generateMode: "onlyTxt",
      importMode: "add",
      dicts: [
        { name: "custom", isUsing: true, priority: 99 },
        { name: "baidu", isUsing: true, priority: 89 },
        { name: "persons", isUsing: false, priority: 79 },
        { name: "complete", isUsing: false, priority: 69 },
        { name: "modern", isUsing: false, priority: 59 },
      ],
    });
  }
}

ipcMain.on("setProgress", (ev, data) => {
  if (data.progress !== data.total) {
    win.setProgressBar(data.progress / data.total);
  } else {
    win.setProgressBar(-1);
    ev.sender.send("done", data);
  }
});
ipcMain.on("checkPackaged", (event, arg) => {
  event.reply("getPackaged", app.isPackaged);
});
ipcMain.on("checkPackaged2", (event, arg) => {
  event.reply("getPackaged2", app.isPackaged);
});
ipcMain.on("tipclose", (_) => {
  tipWin.close();
});
ipcMain.on("settingclose", (_) => {
  settingWin.close();
});
ipcMain.on("sendCustomFilePath", (event, arg) => {
  event.reply("getCustomFilePath", arg);
});
ipcMain.on("opensetting", (_) => {
  settingWin = new BrowserWindow({
    width: 400,
    height: 600,
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true,
    },
  });
  if (!app.isPackaged) settingWin.webContents.openDevTools(); //启动自动打开控制台
  settingWin.on("closed", () => {
    settingWin = null;
  });
  settingWin.loadFile("setting.html");
});
